package com.lanou.musicproject.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.lanou.musicproject.R;
import com.lanou.musicproject.bean.Constant;
import com.lanou.musicproject.bean.MusicRootNew;
import com.lanou.musicproject.volley.VolleySingleton;

import org.json.JSONObject;

/**
 * Created by dllo on 15/10/26.
 * 歌手信息
 */
public class SingerDescribFragment extends Fragment {
    private long singer_id;// 接收传来的bundle里的值,拼接网址解析
    private String url; // 解析url
    private VolleySingleton volleySingleton;
    private MusicRootNew.DataEntity data;
    private TextView synopsis, singerName, achievements, englishName, birthDate, nationality, nation, birthPlace, height, weight, bloodType, school, commissionBroker, genre, profession;
    private String brief;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_singer_describ, null);
        initView(v);
        return v;
    }

    //初始化控件
    private void initView(View v) {
        synopsis = (TextView) v.findViewById(R.id.singerdescrib_synopsis);
        singerName = (TextView) v.findViewById(R.id.singerdescrib_chinese_name);
        achievements = (TextView) v.findViewById(R.id.singerdescrib_alias);
        englishName = (TextView) v.findViewById(R.id.singerdescrib_english);
        birthDate = (TextView) v.findViewById(R.id.singerdescrib_brithdate);
        nationality = (TextView) v.findViewById(R.id.singerdescrib_nationality);
        nation = (TextView) v.findViewById(R.id.singerdescrib_nation);
        birthPlace = (TextView) v.findViewById(R.id.singerdescrib_brithplace);
        height = (TextView) v.findViewById(R.id.singerdescrib_height);
        weight = (TextView) v.findViewById(R.id.singerdescrib_weight);
        bloodType = (TextView) v.findViewById(R.id.singerdescrib_blood_type);
        school = (TextView) v.findViewById(R.id.singerdescrib_school);
        commissionBroker = (TextView) v.findViewById(R.id.singerdescrib_commission_broker);
        genre = (TextView) v.findViewById(R.id.singerdescrib_genre);
        profession = (TextView) v.findViewById(R.id.singerdescrib_profession);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Bundle bundle = getArguments();
        singer_id = bundle.getLong("singer");
        url = Constant.SINGER_INFOR + singer_id;

        getData(url);
    }

    //获取数据
    private void getData(String url) {
        volleySingleton = VolleySingleton.getInstance(getActivity());
        JsonObjectRequest jr = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                String str = response.toString();
                Gson gson = new Gson();
                MusicRootNew root = gson.fromJson(str, MusicRootNew.class);
                data = root.getData();
                if (data != null) {
                    setViewContent(data);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(jr);
    }

    private void setViewContent(MusicRootNew.DataEntity data) {

        synopsis.setText(data.getBrief());
        singerName.setText(data.getChineseName());
        achievements.setText(data.getAchievements());
        englishName.setText(data.getEnglishName());
        birthDate.setText(data.getBirthDate());
        nationality.setText(data.getCountry());
        nation.setText(data.getNation());
        birthPlace.setText(data.getBirthPlace());
        height.setText(data.getHeight() + "米");
        weight.setText(data.getWeight() + "千克");
        bloodType.setText(data.getBloodType());
        school.setText(data.getSchool());
        commissionBroker.setText(data.getCompanyName());
        profession.setText(data.getCareer());
    }

}
